package com.huadao.cart_ccs.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.main.CartApplication;

public class LiquidPumpView extends RelativeLayout {

    private int mHeight;
    private int mWidth;

    private int mCylinderHeight;
    private int mCylinderWidth;
    private int maxAmount = 500;
    private int currentAmount ;

    private String indicate;

    public LiquidPumpView(Context context) {
        super(context);
        setWillNotDraw(false);
    }

    public LiquidPumpView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(false);
    }

    public LiquidPumpView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setWillNotDraw(false);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();

        mCylinderWidth= (int) (3/4f*mWidth);
        mCylinderHeight= (int) (3/4f*mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.translate((float) (mWidth-mCylinderWidth)/2,(float) (mHeight-mCylinderHeight)/2);

        int bodyStroke = 10;
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(bodyStroke);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);

        //量筒外形
        Path path = new Path();
        path.moveTo(1 / 4f * mCylinderWidth, 0);
        path.lineTo(1 / 4f * mCylinderWidth, mCylinderHeight - 1 / 4f * mCylinderWidth);
        path.lineTo(0, mCylinderHeight);
        path.lineTo(mCylinderWidth, mCylinderHeight);
        path.lineTo(3 / 4f * mCylinderWidth, mCylinderHeight - 1 / 4f * mCylinderWidth);
        path.lineTo(3 / 4f * mCylinderWidth, 0);

        if (currentAmount != 0) {
            //液体
            Paint paintRect = new Paint();
            paintRect.setColor(CartApplication.getInstance().getResources().getColor(R.color.liquid));
            paintRect.setStrokeWidth(5);
            paintRect.setAntiAlias(true);
            paintRect.setStyle(Paint.Style.FILL);

            Rect rect = new Rect();
            int left = (int) (1 / 4f * mCylinderWidth);
            int top = (int) (mCylinderHeight - (float) currentAmount / maxAmount * mCylinderHeight);
            int right = (int) (3 / 4f * mCylinderWidth);
            int bottom = mCylinderHeight;
            rect.set(left, top, right, bottom);

            canvas.drawRect(rect, paintRect);
            int xOffset=10;
            String amount;
            if(currentAmount!=maxAmount){
                amount=String.valueOf(currentAmount);
            }else {
                StringBuilder stringBuilder=new StringBuilder();
                stringBuilder.append(currentAmount);
                stringBuilder.append(" ");
                stringBuilder.append(CartApplication.getInstance().getResources().getString(R.string.full));
//                amount=currentAmount+"("+"满"+")";
                amount=stringBuilder.toString();
            }
            Paint paintText = new Paint();
            paintText.setColor(Color.BLACK);
            paintText.setAntiAlias(true);
            paintText.setTextSize(30);
            paintText.setStyle(Paint.Style.STROKE);
            canvas.drawText(amount, 3 / 4f * mCylinderWidth+xOffset, top, paintText);
        }
        canvas.drawPath(path, paint);
        canvas.drawLine(1 / 4f * mCylinderWidth, mCylinderHeight - 1 / 4f * mCylinderWidth, 1 / 4f * mCylinderWidth, mCylinderHeight, paint);
        canvas.drawLine(3 / 4f * mCylinderWidth, mCylinderHeight - 1 / 4f * mCylinderWidth, 3 / 4f * mCylinderWidth, mCylinderHeight, paint);

        if(indicate!=null) {
            canvas.restore();

            Paint paintText = new Paint();
            paintText.setColor(Color.BLACK);
            paintText.setAntiAlias(true);
            paintText.setTextSize(30);
            paintText.setStyle(Paint.Style.STROKE);
            paintText.setTextAlign(Paint.Align.CENTER);

            int x= (int) (1 / 2f * mWidth-indicate.length()/2);
            int y= (int) (mHeight-(float)(mHeight-mCylinderHeight)/4)+10;
            canvas.drawText(indicate, x, y, paintText);
        }
    }

    public void setCurrentLiquid(int liquid) {
        if(liquid<0){
            this.currentAmount=0;
        }else {
            this.currentAmount = liquid;
        }
        invalidate();
    }

    public void setIndicate(String indicate) {
        this.indicate = indicate;
        invalidate();
    }

    public void setMaxAmount(int maxAmount) {
        this.maxAmount = maxAmount;
    }
}
